package com.crud.web; /**
 * @author wzm
 * @create 2023-05-08 10:39
 */

import com.crud.bean.Employee;
import com.crud.service.EmployeeService;
import com.crud.service.impl.EmployeeServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/findAll")
public class ListServlet extends HttpServlet {
    private EmployeeService employeeService = new EmployeeServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        List<Employee> byAll = employeeService.findByAll();
        if (byAll != null) {
            //将数据存储到域对象中显示到页面上
            request.setAttribute("empList",byAll);
            request.getRequestDispatcher("/employee_list.jsp").forward(request,response);

        }else{
            response.getWriter().write("抱歉，内部错误，稍后重试！！");
        }

    }
}
